package com.example.springboot_test.common.poi.util;

import cn.hutool.core.collection.CollUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;

import java.util.List;

/**
 * POI Word工具类
 *
 * @author xudongmaster
 */
public class POIWordUtil {

    /**
     * 填充文本内容，保留单元格样式(07版单元格)
     * 单元格没有内容时事先写入空格，也可以保留单元格样式
     *
     * @param cell 单元格
     * @param text 文本内容
     */
    public static void setCellText(XWPFTableCell cell, String text) {
        try {
            cell.getParagraphArray(0).getRuns().get(0).setText(text);
        } catch (Exception e) {
            cell.setText(text);
        }
    }

    /**
     * 行合并
     *
     * @param table         表格对象
     * @param beginRowIndex 开始行索引
     * @param endRowIndex   结束行索引
     * @param colIndex      合并列索引
     */
    public static void rowMerge(XWPFTable table, int beginRowIndex, int endRowIndex, int colIndex) {
        if (beginRowIndex == endRowIndex || beginRowIndex > endRowIndex) {
            return;
        }
        //合并行单元格的第一个单元格
        CTVMerge startMerge = CTVMerge.Factory.newInstance();
        startMerge.setVal(STMerge.RESTART);
        //合并行单元格的第一个单元格之后的单元格
        CTVMerge endMerge = CTVMerge.Factory.newInstance();
        endMerge.setVal(STMerge.CONTINUE);
        table.getRow(beginRowIndex).getCell(colIndex).getCTTc().getTcPr().setVMerge(startMerge);
        for (int i = beginRowIndex + 1; i <= endRowIndex; i++) {
            table.getRow(i).getCell(colIndex).getCTTc().getTcPr().setVMerge(endMerge);
        }
    }

    /**
     * 复制单元格和样式
     *
     * @param targetRow 要复制的行
     * @param sourceRow 被复制的行
     */
    public static void createCellsAndCopyStyles(XWPFTableRow targetRow, XWPFTableRow sourceRow) {
        targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
        List<XWPFTableCell> tableCells = sourceRow.getTableCells();
        if (CollUtil.isEmpty(tableCells)) {
            return;
        }
        for (XWPFTableCell sourceCell : tableCells) {
            XWPFTableCell newCell = targetRow.addNewTableCell();
            newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
            List sourceParagraphs = sourceCell.getParagraphs();
            if (CollUtil.isEmpty(sourceParagraphs)) {
                continue;
            }
            XWPFParagraph sourceParagraph = (XWPFParagraph) sourceParagraphs.get(0);
            List targetParagraphs = newCell.getParagraphs();
            XWPFParagraph targetParagraph = CollUtil.isEmpty(targetParagraphs) ? newCell.addParagraph() : (XWPFParagraph) targetParagraphs.get(0);
            targetParagraph.getCTP().setPPr(sourceParagraph.getCTP().getPPr());
            XWPFRun targetRun = targetParagraph.getRuns().isEmpty()
                    ? targetParagraph.createRun() : targetParagraph.getRuns().get(0);
            List<XWPFRun> sourceRunList=sourceParagraph.getRuns();
            if (CollUtil.isNotEmpty(sourceRunList)) {
                XWPFRun sourceRun=sourceRunList.get(0);
                //字体名称
                targetRun.setFontFamily(sourceRun.getFontFamily());
                //字体大小
                targetRun.setFontSize(sourceRun.getFontSize());
                //字体颜色
                targetRun.setColor(sourceRun.getColor());
                //字体加粗
                targetRun.setBold(sourceRun.isBold());
                //字体倾斜
                targetRun.setItalic(sourceRun.isItalic());
            }
        }
    }
}
